package ControlLayer;
import java.util.ArrayList;
import ModelLayer.*;
import java.util.Date;
import java.text.DateFormat;
//model layer package has to be imported
/**
 * Controller class for the DVD collection
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * @revised version English (Ab)
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class DVDCtr
{
    //declare instance variables to reference containers
    //passed as arguments from the main class
    private DVDContainer DVDCollection;
    private Rental rent;
    
    /**
     * Constructor for objects of class DVDCtr
     */
    public DVDCtr()//DVD container is to be passed as parameter from the main controller
    {
        //set up references to containers  
        DVDCollection = new DVDContainer();
    }
    
    //Dummy implementation: after implementing the model layer 
    //objects of class DVD are to be returned, not String
    public DvD findDVD(int _id)
    {
        //this method is to search the container for a DVD
        //with an id-number equal to the parameter no
        //when the dvd is found, the method is to call print-methods in the TUI layer
       return DVDCollection.findDVD(_id);
      // System.out.println(DVDCollection.findDVD(_id)); 
    }
    
    //Dummy implementation: more parameters are probaly to added
    public void createDVD(int _id, String _title, String _artist, String _releaseDate)
    {       
          //this method constructs a new dvd object 
          //and stores it in the container
          if(DVDCollection.findDVD(_id) != null)
          {
              if(DVDCollection.findDVD(_id).getId() == _id)
              {
                  System.out.println("Id already exists.");
              }
              else
              {
                  DvD d = DVDCollection.createDVD(_id, _title, _artist, _releaseDate);
                  DVDCollection.addDVD(d);
                  System.out.println("Done.");
              }
              
          }
          else
          {
              DvD d = DVDCollection.createDVD(_id, _title, _artist, _releaseDate);
              DVDCollection.addDVD(d);
              System.out.println("Done.");
          }
    }
    
    public void updateDVD(int _id, String _title, String _artist, String _releaseDate)
    {
        DvD d = DVDCollection.findDVD(_id);
        if(!test(_title, ""))
        {
            d.setTitle(_title);
        }
        if(!test(_artist, ""))
        {
            d.setArtist(_artist);
        }
        if(!test(_releaseDate, ""))
        {
            d.setReleaseDate(_releaseDate);
        }
    }
    
    public void deleteDVD(int _id)
    {
        
        //search for the dvd and delete the object from the container
        DVDCollection.removeDVD(_id);
    }
    
    public void createSample(int _id, int serialNr, String acqDate, double acqPrice)
    {
        //create a copy of the specified dvd and add it to the dvdsample collection
        DvD aCopy = DVDCollection.findDVD(_id);
        DVDSample dvdSample = aCopy.createDVDSample(serialNr, acqDate, acqPrice);
        aCopy.addDVD(dvdSample);
        
    }
    
    private boolean test(String x, String y)
    {
        boolean result = false;
        if(x.equals(y))
        {
            result = true;
        }
        return result;
    }
    
    public ArrayList<String> listAllDvds()
    {
          //returns a list of all dvds and their associated copies
          return DVDCollection.showAllDVDs();
    }
    
    public int getDVDID(int _id)
    {
        DvD d = DVDCollection.findDVD(_id);
        return d.getId();
    }
}
